function initCanvas() {
    initdisp();
    default_scene();
}

function onSizeChange() {
    var x =document.config.xres.value;
    var y =document.config.yres.value;
    var canvas = document.getElementById("frame");
    canvas.width = x;
    canvas.height = y;
    display.xres = x;
    display.yres = y;
    display.context = canvas.getContext("2d");
    initdisp();
    startRender();
}

function startRender() {
    if(isNaN(render.ray_trace.xres)) render.ray_trace.xres = 2;
    if(isNaN(render.ray_trace.yres)) render.ray_trace.yres = 2;
    render_display();
}

function changeTraceResolution() {
    var xres = document.config.rend_xres.value;
    var yres = document.config.rend_yres.value;
    if(isNaN(xres) || isNaN(yres)) {
	return;
    }
    xres = parseInt(xres);
    yres = parseInt(yres);
    render.ray_trace.xres = xres;
    render.ray_trace.yres = yres;
}

function changeLighting() {
    if(document.config.ambient.checked) render.lighting.ambient = true;
    else render.lighting.ambient = false;
    if(document.config.diffuse.checked) render.lighting.diffuse = true;
    else render.lighting.diffuse = false;
    if(document.config.specular.checked) render.lighting.specular = true;
    else render.lighting.specular = false;
}

function changeTraceDepth() {

	var traceFactor = document.config.traceDepth.value;
	// alert(traceFactor);
	if(isNaN(traceFactor)) {
		alert("Please enter a number!");
		return;
    }
    
    traceFactor = parseInt(traceFactor);
    render.ray_trace.trace_depth = traceFactor;
}

function changeAntiAliasing() {

    if(document.config.multipass.checked) {
    	render.isMultiPass = true;
    }
    
    else {
    	render.isMultiPass = false;
    }

	var AAfactor = document.config.supersampling.value;
	
	if(isNaN(AAfactor)) {
		alert("Please enter a number!");
		return;
    }
    
    AAfactor = parseInt(AAfactor);
    render.ANTI_ALIASING_FACTOR = AAfactor;
}

function changeFilter(ratio) {
    filter.type = parseInt(ratio.value);
}

function post_process() {
    boxfiltering(true);
}

window.onload = initCanvas;